package com.wjh.web.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.wjh.base.ResponseApiData;
import com.wjh.order.dto.OrderDetailDto;
import com.wjh.order.request.UpdateOrderPayStateRequest;
import com.wjh.service.IOrderService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单对外 API接口
 * 
 * @author wenjianhai
 * @date 2023/6/26
 * @since JDK 17
 */
@Tag(name = "订单对外 AIP接口", description = "订单对外 AIP接口")
@Slf4j
@RestController
@RequestMapping("/order/api")
public class OrderApiController {

	@Autowired
	private IOrderService orderService;

	@Operation(summary = "查询订单及其详情", description = "查询订单及其详情",
			parameters = {@Parameter(name = "orderGuid", description = "订单Guid")})
	@GetMapping("/queryOrderAndDetails/{orderGuid}")
	public ResponseApiData<OrderDetailDto> queryOrderAndDetails(@PathVariable String orderGuid) {
		log.info("查询订单及其详情-开始.订单Guid:{}", orderGuid);
		if (StringUtils.isBlank(orderGuid)) {
			log.info("查询订单及其详情,订单Guid为空");
			return ResponseApiData.parameterFail("订单Guid不能为空");
		}
		try {
			return ResponseApiData.success(orderService.queryOrderAndDetails(orderGuid));
		} catch (Exception e) {
			log.error(String.format("查询订单及其详情-失败.订单Guid:%s", orderGuid), e);
			return ResponseApiData.systemFail("查询失败");
		}
	}

	@Operation(summary = "更新订单的支付状态", description = "更新订单的支付状态")
	@PostMapping("/update/payState")
	public ResponseApiData<?> updatePayState(@Validated @RequestBody UpdateOrderPayStateRequest request) {
		log.info("更新订单的支付状态-开始.请求参数:{}", JSON.toJSONString(request));
		try {
			orderService.updatePayState(request);
			log.info("更新订单的支付状态-结束.请求参数:{}", JSON.toJSONString(request));
			return ResponseApiData.success();
		} catch (Exception e) {
			log.error(String.format("更新订单的支付状态-失败.请求参数:%s", JSON.toJSONString(request)), e);
			return ResponseApiData.systemFail("更新订单的支付状态失败:" + e.getMessage());
		}
	}
}
